import com.baidu.echarts.Option;
import com.baidu.echarts.Title;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.org.apache.xerces.internal.impl.xpath.XPath;
import org.apache.hdfs.FileUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

import com.baidu.echarts.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.hdfs.FileUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@WebServlet("/topn")
public class TopNTemServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("content-type", "text/json;charset=utf-8");
//        resp.getWriter().write("{\"series\":{\"name\": \"访问来源\",\"type\": \"pie\",\"radius\": \"55%\",\"center\": [\"50%\", \"60%\"],\"data\": [{\"value\": 335,\"name\": \"直接访问\"},{\"value\": 310,\"name\": \"邮件营销\"},{\"value\": 234,\"name\": \"联盟广告\"},{\"value\": 135,\"name\": \"视频广告\"},{\"value\": 1548,\"name\": \"搜索引擎\"}]},\"legend\":[\"直接访问\", \"邮件营销\", \"联盟广告\", \"视频广告\", \"搜索引擎\"]}");
        resp.getWriter().write(testEcharts());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("content-type", "text/json;charset=utf-8");
        resp.getWriter().write("{\"series\":{\"name\": \"访问来源\",\"type\": \"pie\",\"radius\": \"55%\",\"center\": [\"50%\", \"60%\"],\"data\": [{\"value\": 233,\"name\": \"直接访问N\"},{\"value\": 122,\"name\": \"邮件营销N\"},{\"value\": 54,\"name\": \"联盟广告N\"},{\"value\": 388,\"name\": \"视频广告N\"},{\"value\": 2112,\"name\": \"搜索引擎N\"}]},\"legend\":[\"直接访问N\", \"邮件营销N\", \"联盟广告N\", \"视频广告N\", \"搜索引擎N\"]}");

    }

    private String testEcharts() throws IOException {

        ObjectMapper mapper = new ObjectMapper();
        Option option = new Option();


        Map<Integer, String[]> SS= FileUtil.ReadPartFile("D:\\topnoutput\\part-r-00000.txt");
        String[] line0=SS.get(0);     //第0行
        String[] line1=SS.get(1);     //第1行
        String[] line2=SS.get(2);     //第2行
        String[] line3=SS.get(3);     //第3行
        String[] line4=SS.get(4);     //第4行
        String[] line5=SS.get(5);     //第5行
        String[] line6=SS.get(6);     //第6行
        String[] line7=SS.get(7);     //第7行
        String[] line8=SS.get(8);     //第8行
        String[] line9=SS.get(9);     //第9行

        String cell00=line0[0];        //第0行的第0个数据


        Title title = new Title();
        title.setText("1743年到2013年全球地表温度");
        title.setSubtext("温度最高的前十位");
        title.setX("center");
        option.setTitle(title);

        Tooltip tooltip = new Tooltip();
        tooltip.setTrigger("axis");
        AxisPointer aa=new AxisPointer();
        aa.setType("shadow");
        tooltip.setAxisPointer(aa);
        option.setTooltip(tooltip);


        List<XYAxis> xAxisList = new ArrayList<>();
        XYAxis axis1=new XYAxis();
        axis1.setData(Arrays.asList(new String[]{
                line9[0]+"\n"+line9[3]+"-"+line9[4]+"\n\n"+"纬度："+line9[5]+"---"+"经度："+line9[6],
                line8[0]+"\n"+line8[3]+"-"+line8[4]+"\n\n"+"纬度："+line8[5]+"---"+"经度："+line8[6],
                line7[0]+"\n"+line7[3]+"-"+line7[4]+"\n\n"+"纬度："+line7[5]+"---"+"经度："+line7[6],
                line6[0]+"\n"+line6[3]+"-"+line6[4]+"\n\n"+"纬度："+line6[5]+"---"+"经度："+line6[6],
                line5[0]+"\n"+line5[3]+"-"+line5[4]+"\n\n"+"纬度："+line5[5]+"---"+"经度："+line5[6],
                line4[0]+"\n"+line4[3]+"-"+line4[4]+"\n\n"+"纬度："+line4[5]+"---"+"经度："+line4[6],
                line3[0]+"\n"+line3[3]+"-"+line3[4]+"\n\n"+"纬度："+line3[5]+"---"+"经度："+line3[6],
                line2[0]+"\n"+line2[3]+"-"+line2[4]+"\n\n"+"纬度："+line2[5]+"---"+"经度："+line2[6],
                line1[0]+"\n"+line1[3]+"-"+line1[4]+"\n\n"+"纬度："+line1[5]+"---"+"经度："+line1[6],
                line0[0]+"\n"+line0[3]+"-"+line0[4]+"\n\n"+"纬度："+line0[5]+"---"+"经度："+line0[6]
        }));
        xAxisList.add(axis1);

        XYAxis axis2=new XYAxis();
        axis2.setType("category");
        xAxisList.add(axis2);
        option.setxAxis(xAxisList);


        List<XYAxis> yAxisList = new ArrayList<>();


        XYAxis axis3=new XYAxis();
        /**/
        axis3.setData(Arrays.asList(new String[]{"35",line9[1],line8[1],line7[1],line6[1],line5[1],line4[1],line3[1],line2[1],line1[1],line0[1]}));
        yAxisList.add(axis3);


        XYAxis axis4=new XYAxis();
        axis4.setType("category");
        yAxisList.add(axis4);
        option.setyAxis(yAxisList);



        option.setCalculable(true);

        Series series = new Series();
        series.setName("温度");
        series.setType("bar");


        List<Series> seriesList = new ArrayList<>();
        List<SeriesData> data = new ArrayList<>();
        SeriesData data1 = new SeriesData();

        data1 = new SeriesData();
        data1.setName("温度");
        data1.setValue(line9[1]);
        data.add(data1);

        data1 = new SeriesData();
        data1.setName("温度");
        data1.setValue(line8[1]);
        data.add(data1);


        data1 = new SeriesData();
        data1.setName("温度");
        data1.setValue(line7[1]);
        data.add(data1);

        data1 = new SeriesData();
        data1.setName("温度");
        data1.setValue(line6[1]);
        data.add(data1);


        data1 = new SeriesData();
        data1.setName("温度");
        data1.setValue(line5[1]);
        data.add(data1);

        data1 = new SeriesData();
        data1.setName("温度");
        data1.setValue(line4[1]);
        data.add(data1);

        data1 = new SeriesData();
        data1.setName("温度");
        data1.setValue(line3[1]);
        data.add(data1);

        data1 = new SeriesData();
        data1.setName("温度");
        data1.setValue(line2[1]);
        data.add(data1);


        data1 = new SeriesData();
        data1.setName("温度");
        data1.setValue(line1[1]);
        data.add(data1);

        data1.setName("温度");
        data1.setValue(line0[1]);
        data.add(data1);

        series.setData(data);
        seriesList.add(series);
        option.setSeries(seriesList);

        System.out.println(mapper.writeValueAsString(option));
        return mapper.writeValueAsString(option);
    }

}
